package com.sunda.spmsoversea.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sunda.spmsoversea.dto.OverseaRequisitionDtlDTO;
import com.sunda.spmsoversea.entity.OverseaRequisitionDtl;
import com.sunda.spmsoversea.mapper.OverseaRequisitionDtlMapper;
import com.sunda.spmsoversea.service.IOverseaOperateLogService;
import com.sunda.spmsoversea.service.IOverseaRequisitionDtlService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import static com.sunda.spmsoversea.enums.OverseaRequisitionEnum.REQUISITION_UPDATE;

/**
 * <p>
 * 领用申请表明细 服务实现类
 * </p>
 *
 * @author Wayne
 * @since 2021-09-22
 */
@Service
public class OverseaRequisitionDtlServiceImpl extends ServiceImpl<OverseaRequisitionDtlMapper, OverseaRequisitionDtl> implements IOverseaRequisitionDtlService {

    @Autowired
    IOverseaOperateLogService iOverseaOperateLogService;

    @Override
    public int addRequisitionDtl(OverseaRequisitionDtl overseaRequisitionDtl) {
        if (overseaRequisitionDtl == null){
            return 0;
        }
        return this.baseMapper.insert(overseaRequisitionDtl);
    }

    @Override
    public List<Map<String, Object>> getRequisitionDtlList(String uuidRequisition) {
        if (StringUtils.isEmpty(uuidRequisition)){
            return null;
        }
        return this.baseMapper.getRequisitionDtlList(uuidRequisition);
    }

    @Override
    public void deleteOverseaRequisitionDtl(String uuidRequisition, Integer item, String userId) {
        if (StringUtils.isEmpty(uuidRequisition) || item == null){
            return;
        }
        QueryWrapper<OverseaRequisitionDtl> queryWrapper =
                new QueryWrapper<OverseaRequisitionDtl>().eq("UUID_REQUISITION", uuidRequisition).eq("ITEM", item);
        this.baseMapper.delete(queryWrapper);
        iOverseaOperateLogService.addOverseaOperateLog(userId, uuidRequisition, REQUISITION_UPDATE.getDescZh(), REQUISITION_UPDATE.getDescEn(), "");
    }

    @Override
    public synchronized int addOverseaRequisitionDtl(OverseaRequisitionDtlDTO overseaRequisitionDtlDTO, String userId) {
        String uuidRequisition = overseaRequisitionDtlDTO.getUuidRequisition();
        if (StringUtils.isEmpty(uuidRequisition) || StringUtils.isEmpty(overseaRequisitionDtlDTO.getMaterialNo())){
            return -1;
        }
        OverseaRequisitionDtl getMaxItem = this.baseMapper.getMaxItem(uuidRequisition);
        if (getMaxItem == null){
            return -1;
        }
        OverseaRequisitionDtl overseaRequisitionDtl = new OverseaRequisitionDtl();
        overseaRequisitionDtl.setUuidRequisition(uuidRequisition);
        overseaRequisitionDtl.setItem(getMaxItem.getItem() + 10);
        overseaRequisitionDtl.setWerks(getMaxItem.getWerks());
        overseaRequisitionDtl.setWorkshopCode(getMaxItem.getWorkshopCode());
        overseaRequisitionDtl.setWhsLocationCode(getMaxItem.getWhsLocationCode());
        overseaRequisitionDtl.setMaterialNo(overseaRequisitionDtlDTO.getMaterialNo());
        overseaRequisitionDtl.setApplyQtyBasicUnit(overseaRequisitionDtlDTO.getApplyQtyBasicUnit());
        overseaRequisitionDtl.setBasicUnit(overseaRequisitionDtlDTO.getBasicUnit());
        overseaRequisitionDtl.setApplyQtyUnitSales(overseaRequisitionDtlDTO.getApplyQtyUnitSales());
        overseaRequisitionDtl.setUnitSales(overseaRequisitionDtlDTO.getUnitSales());
        overseaRequisitionDtl.setRemarks(overseaRequisitionDtlDTO.getRemarks());
        overseaRequisitionDtl.setMovementType(overseaRequisitionDtlDTO.getMovementType());
        overseaRequisitionDtl.setActualMovementType(overseaRequisitionDtlDTO.getActualMovementType());
        int resp = this.baseMapper.insert(overseaRequisitionDtl);
        iOverseaOperateLogService.addOverseaOperateLog(userId, uuidRequisition, REQUISITION_UPDATE.getDescZh(), REQUISITION_UPDATE.getDescEn(), "");
        return resp;
    }

    @Override
    public int addOverseaRequisitionDtls(List<OverseaRequisitionDtlDTO> dtlDTOList, String userId) {
        if (dtlDTOList == null || dtlDTOList.size() == 0){
            return -1;
        }
        String uuidRequisition = dtlDTOList.get(0).getUuidRequisition();
        if (StringUtils.isEmpty(uuidRequisition) || StringUtils.isEmpty(dtlDTOList.get(0).getMaterialNo())){
            return -1;
        }
        OverseaRequisitionDtl getMaxItem = this.baseMapper.getMaxItem(uuidRequisition);
        if (getMaxItem == null){
            return -1;
        }
        int resp = 0;
        for (int i = 0; i < dtlDTOList.size(); i ++){
            OverseaRequisitionDtlDTO dto = dtlDTOList.get(i);
            OverseaRequisitionDtl overseaRequisitionDtl = new OverseaRequisitionDtl();
            overseaRequisitionDtl.setUuidRequisition(uuidRequisition);
            overseaRequisitionDtl.setItem(getMaxItem.getItem() + 10 * (i + 1));
            overseaRequisitionDtl.setWerks(getMaxItem.getWerks());
            overseaRequisitionDtl.setWorkshopCode(getMaxItem.getWorkshopCode());
            overseaRequisitionDtl.setWhsLocationCode(getMaxItem.getWhsLocationCode());
            overseaRequisitionDtl.setMaterialNo(dto.getMaterialNo());
            overseaRequisitionDtl.setApplyQtyBasicUnit(dto.getApplyQtyBasicUnit());
            overseaRequisitionDtl.setBasicUnit(dto.getBasicUnit());
            overseaRequisitionDtl.setApplyQtyUnitSales(dto.getApplyQtyUnitSales());
            overseaRequisitionDtl.setUnitSales(dto.getUnitSales());
            overseaRequisitionDtl.setRemarks(dto.getRemarks());
            overseaRequisitionDtl.setMovementType(dto.getMovementType());
            overseaRequisitionDtl.setActualMovementType(dto.getActualMovementType());
            if (this.baseMapper.insert(overseaRequisitionDtl) > 0){
                resp += 1;
            }
        }
        if (resp > 0){
            iOverseaOperateLogService.addOverseaOperateLog(userId, uuidRequisition, REQUISITION_UPDATE.getDescZh(), REQUISITION_UPDATE.getDescEn(), "");
        }
        return resp;
    }

    @Override
    public int updateRequisitionDtl(OverseaRequisitionDtlDTO overseaRequisitionDtlDTO) {
        if (overseaRequisitionDtlDTO == null){
            return 0;
        }
        String uuidRequisition = overseaRequisitionDtlDTO.getUuidRequisition();
        if (StringUtils.isEmpty(uuidRequisition) || overseaRequisitionDtlDTO.getItem() == null){
            return 0;
        }
        int item = overseaRequisitionDtlDTO.getItem();
        QueryWrapper<OverseaRequisitionDtl> queryWrapper =
                new QueryWrapper<OverseaRequisitionDtl>().eq("UUID_REQUISITION", uuidRequisition).eq("ITEM", item);
        OverseaRequisitionDtl overseaRequisitionDtl = this.baseMapper.selectOne(queryWrapper);
        if (overseaRequisitionDtl == null){
            return 0;
        }
        overseaRequisitionDtl.setApplyQtyBasicUnit(overseaRequisitionDtlDTO.getApplyQtyBasicUnit() == null ? 0.0 : overseaRequisitionDtlDTO.getApplyQtyBasicUnit());
        overseaRequisitionDtl.setApplyQtyUnitSales(overseaRequisitionDtlDTO.getApplyQtyUnitSales() == null ? 0.0 : overseaRequisitionDtlDTO.getApplyQtyUnitSales());
        overseaRequisitionDtl.setActualQtyBasicUnit(overseaRequisitionDtlDTO.getActualQtyBasicUnit() == null ? 0.0 : overseaRequisitionDtlDTO.getActualQtyBasicUnit());
        overseaRequisitionDtl.setActualQtyUnitSales(overseaRequisitionDtlDTO.getActualQtyUnitSales() == null ? 0.0 : overseaRequisitionDtlDTO.getActualQtyUnitSales());
        overseaRequisitionDtl.setRemarks(overseaRequisitionDtlDTO.getRemarks() == null ? "" : overseaRequisitionDtlDTO.getRemarks());
        overseaRequisitionDtl.setMovementType(overseaRequisitionDtlDTO.getMovementType());
        overseaRequisitionDtl.setActualMovementType(overseaRequisitionDtlDTO.getActualMovementType());
        return this.baseMapper.update(overseaRequisitionDtl, queryWrapper);
    }

    @Override
    public List<OverseaRequisitionDtl> getRequisitionDtls(String uuidRequisition) {
        if (StringUtils.isEmpty(uuidRequisition)){
            return null;
        }
        return this.baseMapper.selectList(new QueryWrapper<OverseaRequisitionDtl>().eq("UUID_REQUISITION", uuidRequisition));
    }
}
